from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from try_mysql import *
from PyQt5.uic import loadUiType
import cinema
import sys
ui, _ = loadUiType('sale_record.ui')

class Sale_Record_App(QMainWindow, ui):
    #定义构造
    def __init__(self):
        QMainWindow.__init__(self)
        self.setupUi(self)
        self.handle_buttons()
        self.show()
        self.show_tab()

    def handle_buttons(self):
        self.add_goods_record.clicked.connect(self.Add_goods_record)
        self.drop_goods_record.clicked.connect(self.Drop_goods_record)
        self.query_goods_record.clicked.connect(self.Query_goods_record)
        self.change_goods_record.clicked.connect(self.Update_goods_record)
        self.record_cancel.clicked.connect(self.Close)

    #1.添加类别
    def Add_goods_record(self):
        conn = get_conn()
        cur = conn.cursor()
        GoodsNumber=self.goods_name_2.text()
        GoodsName = self.goods_name.text()
        GoodsSaleDate = self.goods_sale_date.text()
        GoodsSaleSize = self.goods_sale_size.text()
        GoodsPrice = self.goods_price.text()
        GoodsNum = self.goods_num.currentText()
        GoodsAllPrice = self.goods_all_price.text()
        if (GoodsNumber=="" or GoodsName== "" or GoodsSaleDate == "" or GoodsSaleSize == "" or GoodsPrice == "" or GoodsNum == ""):
            QMessageBox.information(self, "提示", "主要字段不得为空", QMessageBox.Yes, QMessageBox.Yes)
            return
        else:
            warning = QMessageBox.warning(self, "提示", "你确定添加吗?", QMessageBox.Yes | QMessageBox.No)
            if warning == QMessageBox.Yes:
                sql = "insert into sale_record(GoodsNumber,GoodsName,GoodsSaleDate,GoodsSaleSize,GoodsPrice,GoodsNum,GoodsAllPrice) VALUES (%s,%s,%s,%s,%s,%s,%s)"
                values = (GoodsNumber,GoodsName, GoodsSaleDate, GoodsSaleSize, GoodsPrice, GoodsNum, GoodsAllPrice)
                cur.execute(sql, values)
                conn.commit()
                close_conn(conn, cur)
                QMessageBox.information(self, "提示", "添加成功!", QMessageBox.Yes, QMessageBox.Yes)
                self.show_tab()
                self.clearEdit()
            return




    def Drop_goods_record(self):
        conn = get_conn()
        cur = conn.cursor()
        GoodsNumber=self.goods_name_2.text()
        if (GoodsNumber==""):
            QMessageBox.information(self, "提示", "主要字段不得为空", QMessageBox.Yes, QMessageBox.Yes)
            return
        else:
            warning = QMessageBox.warning(self, "提示", "你确定删除吗?", QMessageBox.Yes | QMessageBox.No)
            if warning == QMessageBox.Yes:
                sql1 = "delete from sale_record where GoodsNumber=%s"
                cur.execute(sql1, (GoodsNumber,))
                conn.commit()
                close_conn(conn, cur)
                print(QMessageBox.information(self, "提醒", "删除成功", QMessageBox.Yes, QMessageBox.Yes))
                self.show_tab()
                self.clearEdit()
            return


    def Query_goods_record(self):
        conn = get_conn()
        cur = conn.cursor()
        GoodsNumber=self.goods_name_2.text()
        GoodsName = self.goods_name.text()
        GoodsSaleDate = self.goods_sale_date.text()
        GoodsSaleSize = self.goods_sale_size.text()
        GoodsPrice = self.goods_price.text()
        sql1 = "select * from sale_record where GoodsNumber=%s or GoodsName=%s"
        values = (GoodsNumber,GoodsName)
        cur.execute(sql1, values)
        data = cur.fetchall()
        if data:
            self.manage_goods_tab.setRowCount(0)
            self.manage_goods_tab.insertRow(0)
            for row, form in enumerate(data):
                for column, item in enumerate(form):
                    self.manage_goods_tab.setItem(row, column, QTableWidgetItem(str(item)))
                    column += 1
                row_position = self.manage_goods_tab.rowCount()
                self.manage_goods_tab.insertRow(row_position)
                self.statusBar().showMessage('查询结果如下')
                self.clearEdit()
        else:
            print(QMessageBox.information(self, "提醒", "查询无记录", QMessageBox.Yes, QMessageBox.Yes))





    def Update_goods_record(self):
        conn = get_conn()
        cur = conn.cursor()
        GoodsNumber=self.goods_name_2.text()
        GoodsName = self.goods_name.text()
        GoodsSaleDate = self.goods_sale_date.text()
        GoodsSaleSize = self.goods_sale_size.text()
        GoodsPrice = self.goods_price.text()
        GoodsNum = self.goods_num.currentText()
        GoodsAllPrice = self.goods_all_price.text()
        if (GoodsNumber=="" or GoodsName=="" or GoodsSaleDate==""  or GoodsSaleSize=="" or GoodsPrice=="" or GoodsNum==""):
            QMessageBox.information(self, "提示", "请明确更新条件", QMessageBox.Yes, QMessageBox.Yes)
            return
        else:
            warning = QMessageBox.warning(self, "提示", "你确定更新吗?", QMessageBox.Yes | QMessageBox.No)
            if warning == QMessageBox.Yes:
                sql1 = "update sale_record set GoodsName=%s,GoodsSaleDate=%s, GoodsSaleSize=%s, GoodsPrice=%s ,GoodsNum=%s,GoodsAllPrice=%s where GoodsNumber=%s"
                values = (GoodsName, GoodsSaleDate, GoodsSaleSize, GoodsPrice, GoodsNum, GoodsAllPrice, GoodsNumber)
                cur.execute(sql1, values)
                conn.commit()
                close_conn(conn, cur)
                print(QMessageBox.information(self, "提醒", "更新成功", QMessageBox.Yes, QMessageBox.Yes))
                self.show_tab()
                self.clearEdit()
            return


    def show_tab(self):
        conn=get_conn()
        cur=conn.cursor()
        sql="select * from sale_record"
        cur.execute(sql)
        data = cur.fetchall()
        if data:
            self.manage_goods_tab.setRowCount(0)
            self.manage_goods_tab.insertRow(0)
            for row , form in enumerate(data):
                for column ,item in enumerate(form):
                    self.manage_goods_tab.setItem(row,column,QTableWidgetItem(str(item)))
                    column += 1
                row_position=self.manage_goods_tab.rowCount()
                self.manage_goods_tab.insertRow(row_position)



    def Close(self):
        self.Mainwindow = cinema.Cinema_App()
        self.close()
        self.Mainwindow.show()


    def clearEdit(self):
        self.goods_name.clear()
        self.goods_name_2.clear()
        self.goods_sale_date.clear()
        self.goods_sale_size.clear()
        self.goods_price.clear()
        self.goods_all_price.clear()

def main():
    app = QApplication([])
    window = Sale_Record_App()
    window.show()
    app.exec_()

if __name__ == '__main__':
    main()